#
# Package to keep track of notes
#

package NoteHandler;

use strict;
use warnings;

=head1 NAME

NoteHandler - package to keep track of what is done to object

=head1 SYNOPSIS

  use NoteHandler;
  $n = new NoteHandler;

  $n->start(tag)      # note that you have started chewing
  $n->head(string)    # emit this if note() is called
  $n->note(string)    # Make a warn() that you have done something
  $n->done(statistic) # note that you have finished chewing

  $n->report()        # note done() results by statistic

=head1 DESCRIPTION

See above

=head2 EXPORT

None

=head1 AUTHOR

Harald Alvestrand <lt>harald@alvestrand.no<gt>

=head1 SEE ALSO

/dev/null

=cut

sub new {
    
    my $self = {};
    $self->{_statistics} = {};
    bless $self;
    return $self;
}

sub start {
    my $self = shift;
    my $tag = shift;

    if ($self->{_active}) {
	warn "NoteHandler error: Tag $$self{_tag} still active when starting $tag\n";
	$self->done("***UNFINISHED***");
    }
    $self->{_tag} = $tag;
    $self->{_active} = 1;
}

sub head {
    my $self = shift;
    $self->{_head} = shift;
}

sub note {
    my $self = shift;

    if ($self->{_head}) {
	warn "$$self{_tag}: $$self{_head}\n";
	delete $$self{_head};
    }
    warn "$$self{_tag}: ", @_, "\n";
}

sub done {
    my $self = shift;
    my $result = shift;

    if (!$self->{_active}) {
	warn "NoteHandler error: done() called > once on tag $$self{_tag}\n";
    }
    $self->{_active} = 0;
    ++ $self->{_statistics}->{$result};

}

sub report {
    my $self = shift;
    my $key;
    my $total = 0;

    my $stats = $self->{_statistics};
    for $key (sort(keys(%$stats))) {
	print $$stats{$key} + 0, " ", $key, "\n";
	$total += $$stats{$key};
    }
    print "\n";
    print "TOTAL: $total\n";
}

1;
